import 'dart:convert';

import 'ni_base_model.dart';

/// 经纬度信息
class NIGeoPoint implements NIModel {
  static const double CONVERSION_FACTOR = 1000000;

  /// 纬度
  double latitude;

  /// 经度
  double longitude;

  ///
  int latitudeE6;
  int longitudeE6;

  /// NICoordinate构造方法
  NIGeoPoint(
      {required this.latitude,
      required this.longitude,
      this.latitudeE6 = 0,
      this.longitudeE6 = 0}) {
    if (latitudeE6 == 0) latitudeE6 = (latitude * CONVERSION_FACTOR).toInt();
    if (longitudeE6 == 0) longitudeE6 = (longitude * CONVERSION_FACTOR).toInt();
  }

  /// map => NICoordinate
  factory NIGeoPoint.fromJson(String json) {
    Map<String, dynamic> map = jsonDecode(json);
    return NIGeoPoint.fromMap(map);
  }

  factory NIGeoPoint.fromMap(Map map) {
    return NIGeoPoint(
        latitude: map['latitude'],
        longitude: map['longitude'],
        latitudeE6: map.containsKey("latitudeE6") ? map['latitudeE6'] : 0,
        longitudeE6: map.containsKey("longitudeE6") ? map['longitudeE6'] : 0);
  }

  @override
  String toJson() {
    return '{"latitude":$latitude,"longitude":$longitude, "latitudeE6":$latitudeE6,"longitudeE6":$longitudeE6}';
  }

  @override
  Map<String, dynamic> toMap() {
    return {
      "longitude": longitude,
      "latitude": latitude,
      "latitudeE6": latitudeE6,
      "longitudeE6": longitudeE6,
    };
  }
}
